package com.shangcheng.server.controller;

import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.model.vo.result.SmsCouponVo;
import com.shangcheng.server.service.SmsCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/coupon")
public class SmsCouponController {
    @Autowired
    private SmsCouponService couponService;

    /**
     * 根据优惠券名称和类型分页获取优惠券列表
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @param type
     * @return
     */
    @GetMapping("/list")
    public Result list(String name, Integer pageNum, Integer pageSize, Integer type) {
        PageResult pg = couponService.list(name, type, pageNum, pageSize);
        return new Result(200, pg, "操作成功");
    }

    /**
     * 添加优惠券
     *
     * @param couponVo
     * @return
     */
    @PostMapping("/create")
    public Result create(@RequestBody SmsCouponVo couponVo) {
        couponService.create(couponVo);
        Result result = new Result();
        result.setCode(200);
        result.setMessage("操作成功");
        return result;
    }

    /**
     * 获取单个优惠券的详细信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        SmsCouponVo vo = couponService.findById(id);
        return new Result(200, vo, "操作成功");
    }

    /**
     * 修改优惠券
     *
     * @param smsCouponVo
     * @param id
     * @return
     */
    @PostMapping("/update/{id}")
    public Result update(@RequestBody SmsCouponVo smsCouponVo, @PathVariable("id") Integer id) {
        couponService.update(id, smsCouponVo);
        Result result = new Result();
        result.setCode(200);
        result.setMessage("操作成功");
        return result;
    }

    /**
     * 删除优惠券
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        couponService.delete(id);
        Result result = new Result();
        result.setCode(200);
        result.setMessage("操作成功");
        return result;
    }
}
